package masterjava.common.configuration;

import masterjava.common.property.Property;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * MasterJava.ru
 * <p/>
 * User: GKislin
 * Date: 31-Jan-2011
 */
public class MapSetting<T> extends Setting {

    private final Map<String, Property<T>> map = new HashMap<String, Property<T>>();

    public MapSetting(String propName) {
        super(propName);
    }

    protected void initMap(Class<T> clazz) {
        initPropStore();
        Iterator keyIterator = propStore.getKeysIterator();
        while (keyIterator.hasNext()) {
            String key = (String) keyIterator.next();
            Property<T> prop = createPropItem(key, clazz);
            map.put(key, prop);
        }
    }

    protected Property<T> createPropItem(String key, Class<T> clazz) {
        return createProp(key, clazz);
    }

    public Map<String, Property<T>> getMap() {
        return map;
    }

    public Property<T> get(String key) {
        Property<T> prop = map.get(key);
        if (prop == null) {
            throw LOGGER.getIllegalArgumentException("Property " + key + " doesn't configured in " + name);
        }
        return prop;
    }

    public String getAsString(String key) {
        return get(key).get().toString();
    }
}
